package com.demo.function.spring.state.machine;

import com.demo.function.spring.state.machine.event.Event;
import com.demo.function.spring.state.machine.status.Status;
import org.springframework.context.annotation.Configuration;
import org.springframework.statemachine.config.EnableStateMachineFactory;
import org.springframework.statemachine.config.EnumStateMachineConfigurerAdapter;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;

import java.util.EnumSet;

/**
 * 状态机工厂配置
 * @author cyj
 */
@Configuration
@EnableStateMachineFactory
public class StateMachineFactoryConfig extends EnumStateMachineConfigurerAdapter<Status, Event> {

    @Override
    public void configure(StateMachineStateConfigurer<Status, Event> states) throws Exception {
        states.withStates().initial(Status.DRAFT).states(EnumSet.allOf(Status.class));
    }

    @Override
    public void configure(StateMachineTransitionConfigurer<Status, Event> transitions) throws Exception {
        transitions.withExternal()
                .source(Status.DRAFT).target(Status.AUDIT)
                .event(Event.SAVE)
                .and()
                .withExternal()
                .source(Status.AUDIT).target(Status.FINISH)
                .event(Event.PASS)
                .and()
                .withExternal()
                .source(Status.AUDIT).target(Status.DRAFT)
                .event(Event.UN_PASS);
    }
}
